import { Block, MapData } from "../Data/MapData";
import MapView from "./MapView";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class CharView extends cc.Component {
    @property(cc.Node)
    outline: cc.Node = null;

    @property(cc.Node)
    bg: cc.Node = null;

    @property(cc.Label)
    label: cc.Label = null;

    blockData: Block = null;

    @property
    isWaitSelect: boolean = false;

    @property
    _isGray: boolean = true; // 方块是否空白块
    @property
    get isGray() {
        return this._isGray;
    }
    //@property
    set isGray(value) {
        this._isGray = value;
        // if (this.blockData) {
        //     this.blockData.isGray = value;
        // }

        if (value) {
            this.bg.active = false;
        } else {
            this.bg.active = true;
        }
    }

    @property
    _isHidden: boolean = false; // 明字还是暗字
    @property
    get isHidden() { return this._isHidden; }
    //@property
    set isHidden(value) {
        this._isHidden = value;
        //this.blockData.isHidden = value;
        if (this._isHidden) {
            this.bg.color = cc.Color.WHITE;
        } else {
            this.bg.color = cc.Color.GRAY;
        }
        this.label.enabled = !value;
    }

    @property
    _isCharFlipped: boolean = false; // 暗字是否被翻开
    @property
    get isCharFlipped() { return this._isCharFlipped; }
    set isCharFlipped(value) {
        this._isCharFlipped = value;
        // if (this.blockData) {
        //     this.blockData.isCharFlipped = value;
        // }
        if (this.isWaitSelect) {
            this.bg.active = !value;
            this.label.enabled = !value;
        } else {
            if (this._isCharFlipped) {
                this.bg.color = cc.Color.YELLOW;
            } else {
                if (this._isHidden) {
                    this.bg.color = cc.Color.WHITE;
                } else {
                    this.bg.color = cc.Color.GRAY;
                }
            }
            this.label.enabled = value;
        }
    }

    @property
    _isIdiomFlipped: boolean = false; // 暗字所在的整个成语是否被翻开
    @property
    get isIdiomFlipped() { return this._isIdiomFlipped; }
    set isIdiomFlipped(value) {
        this._isIdiomFlipped = value;
        // if (this.blockData) {
        //     this.blockData.isIdiomFlipped = value;
        // }
        if (this._isIdiomFlipped) {
            if (this.blockData.char == this.char) {
                this.bg.color = cc.Color.GREEN;
                this.label.node.color = cc.Color.WHITE;
            } else {
                this.bg.color = cc.Color.YELLOW;
                this.label.node.color = cc.Color.RED;
            }
        } else {
            if (this._isHidden) {
                this.bg.color = cc.Color.WHITE;
            } else {
                this.bg.color = cc.Color.GRAY;
            }
        }
    }

    @property
    _isSelected: boolean = false; // 是否选中
    @property
    get isSelected() { return this._isSelected; }
    set isSelected(value) {
        this._isSelected = value;
        this.outline.active = value;
    }

    @property
    _char: string = ''; // 是否选中
    @property
    get char() { return this._char; }
    set char(value) {
        this._char = value;
        this.label.string = value;
    }

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start() {
        // this.node.on(cc.Node.EventType.MOUSE_UP, () => {
        //     let mapData = MapData.GetInstance().data; // shortcut
        //     let ps = MapData.GetInstance().hiddenPoses; // shortcut

        //     // console.log(`你点中了(${row},${col})节点`);
        //     // this.foo();
        //     if (this.blockData.isWaitSelect) {
        //         if (MapView.instance.selCharView.isCharFlipped) {
        //             return;
        //         }
        //         // UNDONE: 数据层要不要同步更新属性？
        //         MapView.instance.selCharView.isCharFlipped = true;
        //         MapView.instance.selCharView.char = this.blockData.char;

        //         this.node.active = false;

        //         // 1. 判断是否猜对整个成语
        //         for (let iWord = 0; iWord < this.blockData.wordIndices.length; iWord++) {
        //             let myWord = MapData.GetInstance().myLevel.words[iWord];

        //             let isIdiomFlipped = true; // 是否整个成语被翻开
        //             for (let iBlock = 0; iBlock < myWord.blocks.length; iBlock++) {
        //                 let myBlock = myWord.blocks[iBlock];

        //                 // 用排除法，如果一个暗字没有被翻开，说明没有猜完整个成语
        //                 if (myBlock.isHidden && !myBlock.isCharFlipped) {
        //                     isIdiomFlipped = false;
        //                     break;
        //                 }
        //             }

        //             if (isIdiomFlipped) {
        //                 let isIdiomRight = true;

        //                 for (let iBlock = 0; iBlock < myWord.blocks.length; iBlock++) {
        //                     let myBlock = myWord.blocks[iBlock];

        //                     // 用排除法，如果一个暗字没有被翻开，说明没有猜对整个成语
        //                     if (myBlock.char != myBlock.viewData.char) {
        //                         isIdiomRight = false;
        //                         break;
        //                     }
        //                 }

        //                 if (isIdiomRight) {
        //                     for (let iBlock = 0; iBlock < myWord.blocks.length; iBlock++) {
        //                         let myBlock = myWord.blocks[iBlock];

        //                         myBlock.isIdiomFlipped = true;
        //                     }
        //                     // TODO: 跳到下个汉字
        //                 }
        //             }
        //         }
        //     } else if (this.blockData.isHidden) {
        //         MapView.instance.selCharView.isSelected = false;
        //         this.isSelected = true;
        //         MapView.instance.selCharView = this;
        //     }
        // });
    }

    // update (dt) {}
}
